package pwr.zpi.twop;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;

import pwr.zpi.twop.views.GameScreen;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.FPSLogger;

public class GameStarter extends Game {
	private final String path;
	
	private FPSLogger fpsLogger;
	private Screen currentScreen;
	private RendererGL20 renderer;
	private ModelProvider modelProvider;
	private BufferedInputStream inputStream;
	private KeyboardInputHandler inputHandler;
	private GameOfLifeLayer gameOfLife;
	
	public GameStarter(String filePath) {
		super();
		this.path = filePath;
	}

	@Override
	public void create() {
		fpsLogger = new FPSLogger();
		renderer = new RendererGL20();
		
		gameOfLife = new GameOfLifeLayer(128);
		
		inputHandler = new KeyboardInputHandler();
		inputHandler.setGameOfLife(gameOfLife);
		
		renderer.setInputHandler(inputHandler);
		
		try {
			inputStream = new BufferedInputStream(new FileInputStream(path));
			modelProvider = new ModelProvider(inputStream);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		renderer.init(modelProvider);
		gameOfLife.setModelProvider(modelProvider);
		gameOfLife.setRenderer(renderer);
		
		currentScreen = new GameScreen(renderer);
		
		this.setScreen(currentScreen);
	}

	@Override
	public void render() {
		currentScreen = getScreen();
		
		currentScreen.render(Gdx.graphics.getDeltaTime());
		
		fpsLogger.log();
	}

	@Override
	public Screen getScreen() {
		return super.getScreen();
	}

}
